package ACM_Brush;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution673 {
    public static void main(String[] args) {
        String s = new String("ababcbacadefegdehijhklij");
        List<Integer> list = new solution_673().partitionLabels(s);
        list.forEach(l -> System.out.println(l));
    }
}

class solution_673 {
    public List<Integer> partitionLabels(String s) {
        Map<Character, int[]> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!map.containsKey(c)) {
                map.put(c, new int[] { i, i });
            } else
                map.put(c, new int[] { map.get(c)[0], i });
        }

        List<Integer> list = new ArrayList<>();
        int start = 0, end = 0;
        for (int i = 0; i < s.length(); i++) {
            end = Math.max(end, map.get(s.charAt(i))[1]);
            if (i == end) {
                list.add(end - start + 1);
                start = end + 1;
            }
        }
        return list;
    }
}